package cn.gson.boot.controller;


import cn.gson.boot.model.pojos.Role;
import cn.gson.boot.model.pojos.User;
import cn.gson.boot.model.pojos.UserLogin;
import cn.gson.boot.model.pojos.Work;
import cn.gson.boot.model.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/user-list")
    @ResponseBody
    public Map<String,Object> userList(String empName, Integer pageNum, Integer pageSize){
        System.out.println("pageNum:"+pageNum);
        System.out.println("pageSize:"+pageSize);
        Page<Object> obj = PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userService.allusers(empName);

        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",userList);
        return map;
    }

    //按时间段查询
    @RequestMapping("/user-sjlist")
    @ResponseBody
    public List<User> allshijian(String Start, String Over,String empName, HttpSession session){
        System.out.println("开始键:"+Start);
        System.out.println("结束:"+Over);
        System.out.println("员工名称:"+empName);
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        int empId  = 0;
        System.err.println("IDIDIDIDI:"+us.getEmpId());
        if(jueNameList.indexOf("boss")>-1){
            empId = 0;
        }else{
            empId = us.getEmpId();
        }
        return userService.allshijian(Start,Over,empName,empId);
    }

    // 修改客户负责人
    @RequestMapping("/choose-user")
    @ResponseBody
    public void updateUser(Integer[] customIdArr, Integer empId) {
        System.out.println("客户转移批量id："+customIdArr + ","+"用户id：" + empId);
        userService.updateUser(customIdArr,empId);
    }
}
